﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppManagement.Models;
using AppManagement.Helpers;
using AppManagement.Repositories;

namespace AppManagement.Reports
{
    public partial class FrmInBaoCaoDoanhSoTheoBacSy : Form
    {
        private string _mabacsy;
        private DateTime _fromdate;
        private DateTime _todate;

        public FrmInBaoCaoDoanhSoTheoBacSy(string mabacsy, DateTime fromdate, DateTime todate)
        {
            InitializeComponent();
            _mabacsy = mabacsy;
            _fromdate = fromdate;
            _todate = todate;
        }

        public void LoadData()
        {
            long tongtien = 0;
            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("TenDichVu");
            dt.Columns.Add("DonViTinh");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("ThanhTien");

            var tbl = GetLstPhieuKhamDichVu();

            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                dt.Rows.Add((i + 1).ToString(), tbl.Rows[i]["TenDV"].ToString(), "Lần", tbl.Rows[i]["SoLuong"].ToString(), String.Format("{0:#,##0;Nothing}", long.Parse(tbl.Rows[i]["ThanhTien"].ToString())));
                tongtien += long.Parse(tbl.Rows[i]["ThanhTien"].ToString());
            }

            rptDoanhSoTheoBacSi rpt = new rptDoanhSoTheoBacSi();
            rpt.DataDefinition.FormulaFields["BacSy"].Text = "'" + BacSi_NhanVienRepository.SelectByID(_mabacsy).ToString() + "'";
            rpt.DataDefinition.FormulaFields["TuNgay"].Text = "'" + String.Format("{0:dd/MM/yyyy}", _fromdate) + "'";
            rpt.DataDefinition.FormulaFields["DenNgay"].Text = "'" + String.Format("{0:dd/MM/yyyy}", _todate) + "'";
            rpt.DataDefinition.FormulaFields["DoanhSo"].Text = "'" + String.Format("{0:#,##0;Nothing}", tongtien) + " VNĐ'";

            rpt.SetDataSource(dt);
            crystalReportViewer1.ReportSource = rpt;
        }

        public DataTable GetLstPhieuKhamDichVu()
        {
            DBProvider provider = new DBProvider();
            provider.InitDBProvider(GlobalVars.g_strDataSource, GlobalVars.g_strDataBaseName);

            var sqlQuery = "";
            sqlQuery += "Select TenDV, PhiDV, Count(*) As SoLuong, (Count(*) * PhiDV) As ThanhTien ";
            sqlQuery += "From PhieuKham pk, PhieuKham_DichVu As pkdv, DichVu dv ";
            sqlQuery += "Where pk.MaPK = pkdv.MaPK And pkdv.MaDV = dv.MaDV And MaBSNV = '" + _mabacsy + "' And NgayKham >= '" + _fromdate + "' And NgayKham <= '" + _todate + "' ";
            sqlQuery += "Group by TenDV, PhiDV";

            var ds = new DataSet();
            provider.SqlQuery = sqlQuery;
            provider.FillDataSet(ref ds, "PKDV");
            return ds.Tables[0];
        }

        private void FrmInBaoCaoDoanhSoTheoBacSy_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
